package com.peaker.advertisingpro.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 网络访问工具类
 * 
 */
public class NetUtils {

	/**
	 * 判断是否连接网络
	 * 
	 * @param context
	 *            上下文
	 * @return
	 */
	public static boolean isConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		if (networkInfo == null) {
			return false;
		} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {// 2g_3g_4g
			if (IsNetWorkEnable(context)) {
				return true;
			} else {
				return false;
			}
		} else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {// wifi
			if (IsNetWorkEnable(context)) {
				new NetPing().execute();
				if (!ishavenet) {
					Toast.makeText(context, "无法连接网络", Toast.LENGTH_LONG).show();
				}
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public static Boolean ishavenet=true;
	public static class NetPing extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			String s = "";
			s = Ping("www.baidu.com");
			Log.i("ping", s);
			if (s.equals("success")) {
				ishavenet=true;
			}else{
				ishavenet=false;
			}
			return s;
		}

	}

	public static String Ping(String str) {
		String resault = "";
		Process p;
		try {
			// ping -c 3 -w 100 中 ，-c 是指ping的次数 3是指ping 3次 ，-w 100
			// 以秒为单位指定超时间隔，是指超时时间为100秒
			p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + str);
			int status = p.waitFor();

			InputStream input = p.getInputStream();
			BufferedReader in = new BufferedReader(new InputStreamReader(input));
			StringBuffer buffer = new StringBuffer();
			String line = "";
			while ((line = in.readLine()) != null) {
				buffer.append(line);
			}

			if (status == 0) {
				resault = "success";
			} else {
				resault = "faild";
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		return resault;

	}

	/**
	 * 是否wifi
	 * 
	 */
	public static boolean iswifi(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		if (networkInfo == null) {
			return false;
		} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {// 2g_3g_4g
			return false;
		} else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {// wifi
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean IsNetWorkEnable(Context context) {
		try {
			ConnectivityManager connectivity = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity == null) {
				return false;
			}
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info != null && info.isConnected()) {
				// 判断当前网络是否已经连接
				if (info.getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


}
